package org.jeecg.modules.htxincailiao.order.mapper;

import java.util.List;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.jeecg.modules.htxincailiao.order.domain.vo.HTOrderSalesStatisticsVO;
import org.jeecg.modules.htxincailiao.order.domain.vo.HTOrderStatisticsVO;
import org.jeecg.modules.htxincailiao.order.entity.HTOrder;

/**
 * @Description: ht_order
 * @Author: jeecg-boot
 * @Date: 2021-11-08 16:58:42
 * @Version: V1.0
 */
public interface HTOrderMapper extends BaseMapper<HTOrder> {

    @Select("<script>" +
            "SELECT * FROM ht_order WHERE order_sn=#{orderSn}" +
            "</script>")
    HTOrder queryByOrderSn(@Param("orderSn") String orderSn);

    @Select("<script>" +
            "SELECT * FROM ht_order WHERE parent_id=#{parentId}" +
            "</script>")
    List<HTOrder> findAllByParentId(@Param("parentId") String parentId);


    @Select("<script>" +
            "SELECT " +
            "(SELECT SUM(total_amount) FROM ht_order) AS total_order_amount," +
            "(SELECT SUM(IF(DATE_FORMAT(create_time,'%Y-%m-%d') = #{dayTime},total_amount,NULL)) FROM " +
            "ht_order) AS day_total_order_amount," +
            "(SELECT COUNT(id) FROM ht_order) AS total_order_nums, " +
            "(SELECT COUNT( IF(DATE_FORMAT(create_time,'%Y-%m-%d') = #{dayTime},id,NULL)) FROM ht_order) AS day_total_order_nums," +
            "(SELECT COUNT(id) FROM ht_order WHERE order_type > 0 ) AS total_paid_order_nums," +
            "(SELECT COUNT(IF(DATE_FORMAT(create_time,'%Y-%m-%d') = #{dayTime},id,NULL)) FROM ht_order WHERE order_type > 0 ) AS day_total_paid_order_nums" +
            "</script>")
    HTOrderStatisticsVO queryStatistics(@Param("dayTime") String dayTime);

    @Select("<script>" +
            " SELECT DATE_FORMAT(create_time,'%Y-%m') AS monthInterval, SUM(total_amount) AS month_total_order_amount" +
            " " +
            " FROM ht_order " +
            " WHERE <![CDATA[ create_time > #{beginTime} AND create_time < #{endTime}]]> " +
            " GROUP BY  monthInterval" +
            "</script>")
    List<HTOrderSalesStatisticsVO> querySalesStatistics(@Param("beginTime") String beginTime,
                                                        @Param("endTime") String endTime);

}
